<?php


namespace api\controllers;


use api\components\BaseController;
use common\helpers\commonApi;
use common\models\Order;
use common\models\OrderRetreat;
use Yii;

class OrderController extends BaseController
{
    /**
     * 订单列表
     * @return array
     */
    public function actionList()
    {
        $userid = self::$userid;
        $get = Yii::$app->request->get();
        $status = $get['status']??'';
        $page = $get['page']??1;
        $model = new Order();
        $where = ['order_status'=>$status];
        if ($status==6){
            $where = ['refund_status'=>[1,2,3]];
        }
        return $model->getList($userid,$where,$page);
    }

    /**
     * 订单详情
     * @return array
     */
    public function actionDetails()
    {
        $get = Yii::$app->request->get();
        $order_sn = $get['order_sn']??'';
        if (!$order_sn){
            return commonApi::message('请选择要查看的订单',false);
        }
        $model = new Order();
        return $model->getDetails($order_sn,self::$userid);
    }

    /**
     * 继续支付
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function actionContinuePay()
    {
        $post = Yii::$app->request->post();
        $order_sn = $post['order_sn']??'';
        if (!$order_sn){
            return commonApi::message('请选择要继续支付的订单',false);
        }
        $model = new Order();
        return $model->continuePay($order_sn,self::$userid);
    }

    /**
     * 确认订单
     * @return array
     */
    public function actionConfirmDelivery()
    {
        $post = Yii::$app->request->post();
        $order_sn = $post['order_sn']??'';
        if (!$order_sn){
            return commonApi::message('请选择要确认的订单',false);
        }
        $model = new Order();
        return $model->confirmDelivery($order_sn,0,self::$userid);
    }

    /**
     * 退货
     * @return array
     */
    public function actionRetreat()
    {
        $post = Yii::$app->request->post();
        $order_sn = $post['order_sn']??'';
        if (!$order_sn){
            return commonApi::message('请选择要确认的订单',false);
        }
        $model = new OrderRetreat();
        return $model->retreat($order_sn,self::$userid);
    }
}